﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Code;
using System.Linq.Expressions;
using SBACN.WebSite.Models;
using System.Text;

namespace SBACN.WebSite
{
	public partial class WebMap : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			BindData();
		}

		protected void BindData()
		{
			Expression<Func<TB_SystemMap, bool>> f = (p => (p.Code.Length == 4 && p.ShowInMap && p.Status));
			Expression<Func<TB_SystemMap, string>> order = (c => (c.Code));
			var resultM = DAL.DALHelper.Instance.GetList<TB_SystemMap, string>(f, order, true);

			this.Map.DataSource = resultM;
			this.Map.DataBind();
		}

		protected string Second(string Code)
		{
			Expression<Func<TB_SystemMap, bool>> f = (p => (p.Code.Length == 8 && p.Code.StartsWith(Code) && p.ShowInMap && p.Status));
			Expression<Func<TB_SystemMap, string>> order = (c => (c.Code));
			var resultM = DAL.DALHelper.Instance.GetList<TB_SystemMap, string>(f, order, true);

			if (resultM.Count > 0)
			{
				StringBuilder Html = new StringBuilder();
				foreach (var l in resultM)
				{

					string url = "";

					// TODO 外链去code

					Html.Append("<p>");
					//url 
					if ( string.IsNullOrWhiteSpace( l.CURL ))
					{
						// 当前code的下一级url
						url = MUrl(l.Code);
					}
					else
					{
						url = l.CURL + "?st=" + l.Code;
					}

					if (!string.IsNullOrWhiteSpace(url) )
					{
						Html.Append("<a target='_bronk' href='");
						Html.Append(url);
						Html.Append("'>");
						Html.Append(l.CText);
						Html.Append("</a>");
					}
					else
					{
						Html.Append(l.CText);
					}

					Html.Append("</p>");
				}
				return Html.ToString();
			}
			else
			{
				return "";
			}

		}

		protected string MUrl(string code)
		{
			Expression<Func<TB_SystemMap, bool>> f = (p => (p.Code.Length == 12 && p.Code.StartsWith(code) && p.ShowInMap));
			Expression<Func<TB_SystemMap, string>> order = (c => (c.Code));
			var resultM = DAL.DALHelper.Instance.GetList<TB_SystemMap, string>(f, order, true);
			string url = "";
			if (resultM.Count > 0)
			{
				foreach (var l in resultM)
				{
					url = l.CURL + "?st=" + l.Code;
					break;
				}
			}
			else
			{
				url = "";
			}

			return url;
		}

	}
}